﻿using System;
using DevExpress.XtraReports.UI;
using DevExpress.XtraSplashScreen;
using prjKQXS_Report;
using prjKQXS_Report.Doanh_thu;
using prjKQXS_DLL;

namespace prjKQXS_UI.View
{
    public partial class frmDoanhThuLD : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// Tự động được gán khi chọn từ lueDonVi. Giá trị là '%' hoặc là mã đơn vị tương ứng.
        /// </summary>
        char MaDV;
        string TenDV;

        /// <summary>
        /// Tự động được gán khi chọn từ lueCanBo. Giá trị là -1 hoặc cán bộ id tương ứng.
        /// </summary>
        int DMCanBoID;

        /// <summary>
        /// Tự động được gán khi chọn từ lueCanBo.
        /// </summary>
        string TenCB;

        #region Form ==============================================================================
        public frmDoanhThuLD()
        {
            InitializeComponent();
            ucDonVi1.lueDonVi.EditValueChanged += new EventHandler(lueDonVi_EditValueChanged);
            ucCanBo1.lueCanBo.EditValueChanged += new EventHandler(lueCanBo_EditValueChanged);
        }

        private void frmBCTHDTDLLD_Load(object sender, EventArgs e)
        {
            dtpTuNgay.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dtpDenNgay.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

            loadLueDonVi();
        }
        #endregion Form ===========================================================================


        #region Control ===========================================================================
        private void lueDonVi_EditValueChanged(object sender, EventArgs e)
        {
            string madv = ucDonVi1.lueDonVi.GetColumnValue("Ma_DT").ToString().Trim();
            MaDV = madv == "TC" ? '%' : Convert.ToChar(madv);
          
            TenDV = (MaDV == '%' ? "" : ucDonVi1.lueDonVi.GetColumnValue("Ten_DT").ToString().Trim());
            loadCB();
            ucCanBo1.lueCanBo.Enabled = !(ucDonVi1.lueDonVi.ItemIndex == 0);
            if (ucCanBo1.lueCanBo.ItemIndex == 0) lueCanBo_EditValueChanged(null, null);
            else ucCanBo1.lueCanBo.ItemIndex = 0;
        }

        private void lueCanBo_EditValueChanged(object sender, EventArgs e)
        {
            DMCanBoID = (int)ucCanBo1.lueCanBo.GetColumnValue("ID");
            TenCB = DMCanBoID==-1? "": ucCanBo1.lueCanBo.GetColumnValue("Ten_DT").ToString().Trim();
        }

        private void sbtnChon_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(frmWaitForm));

            if (rgLoai.SelectedIndex == 0)
            {
                if (MaDV == '%')
                {
                    rptMauHH03x rpt = new rptMauHH03x();
                    rpt.OnLoadComplete += new rptMauHH03x.OnLoadCompleteHandler(rpt_OnLoadComplete);
                    rpt.Parameters["pFormName"].Value = this.Name;
                    rpt.Parameters["pMaDV"].Value = MaDV;
                    rpt.Parameters["pTenDV"].Value = TenDV;
                    rpt.Parameters["pTuNgay"].Value = dtpTuNgay.Value.Date;
                    rpt.Parameters["pDenNgay"].Value = dtpDenNgay.Value.Date;
                    rpt.ShowRibbonPreviewDialog();
                }
                else
                {
                    if (DMCanBoID == -1)
                    {
                        rptMauHH02x rpt = new rptMauHH02x();
                        rpt.OnLoadComplete += new rptMauHH02x.OnLoadCompleteHandler(rpt_OnLoadComplete);
                        rpt.Parameters["pFormName"].Value = this.Name;
                        rpt.Parameters["pTuNgay"].Value = dtpTuNgay.Value.Date;
                        rpt.Parameters["pDenNgay"].Value = dtpDenNgay.Value.Date;
                        rpt.Parameters["pMaDV"].Value = MaDV;
                        rpt.ShowRibbonPreviewDialog();
                    }
                    else
                    {
                        rptDTLuuDong_TH rpt = new rptDTLuuDong_TH();
                        rpt.OnLoadComplete += new rptDTLuuDong_TH.OnLoadCompleteHandler(rpt_OnLoadComplete);
                        rpt.Parameters["pTuNgay"].Value = dtpTuNgay.Value.Date;
                        rpt.Parameters["pDenNgay"].Value = dtpDenNgay.Value.Date;
                        rpt.Parameters["pMaDV"].Value = MaDV;
                        rpt.Parameters["pTenDV"].Value = ucDonVi1.lueDonVi.Text;
                        rpt.Parameters["pDMCanBoID"].Value = DMCanBoID;
                        rpt.Parameters["pTenCB"].Value = TenCB;
                        rpt.ShowRibbonPreviewDialog();
                    }
                }
            }
            else
            {
                rptDTLuuDong_LT rpt = new rptDTLuuDong_LT();
                rpt.OnLoadComplete += new rptDTLuuDong_LT.OnLoadCompleteHandler(rpt_OnLoadComplete);
                rpt.Parameters["pTuNgay"].Value = dtpTuNgay.Value.Date;
                rpt.Parameters["pDenNgay"].Value = dtpDenNgay.Value.Date;
                rpt.pMaDV.Value = MaDV;
                rpt.xrlblDonVi.Text = TenDV == "" ? "" : "PHÒNG XSKT " + TenDV.ToUpper();
                rpt.pDMCanBoID.Value = DMCanBoID;
                rpt.xrlblCanBo.Text = DMCanBoID == -1 ? "" : "Người quản lý trực tiếp: " + TenCB;
                rpt.ShowRibbonPreviewDialog();
            }
        }
        #endregion Control ========================================================================


        #region Sub and Func ======================================================================
        private void rpt_OnLoadComplete()
        {
            SplashScreenManager.CloseForm();
        }

        private void loadLueDonVi()
        {
            ucDonVi1.lueDonVi.Properties.DataSource = clsCommon.load_lueDonVi();

            if (BienTc.isQuanHuyen || BienTc.isThanhPho || !clsCommon.hasPermission(BienTc.UserID, (int)BienTc.eFunction.L_DonVi))
            {
                ucDonVi1.lueDonVi.ItemIndex = ucDonVi1.lueDonVi.Properties.GetDataSourceRowIndex("Ma_DT", BienTc.ma_huyen == "" ? "TC" : BienTc.ma_huyen);
                if (BienTc.isQuanHuyen || BienTc.isThanhPho) ucDonVi1.lueDonVi.Properties.ReadOnly = true;
            }
            else ucDonVi1.lueDonVi.ItemIndex = 0;
        }

        public void loadCB()
        {
            ucCanBo1.lueCanBo.Properties.DataSource = clsCommon.load_lueCanBo(MaDV);
        }
        #endregion Sub and Func ===================================================================
    }
}